package org.example.controller;

import org.example.config.FileConfig;
import org.example.config.R;
import org.example.domain.Chunk;
import org.example.domain.FileResult;
import org.example.domain.Files;
import org.example.service.FilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("files")
public class FileController {


   @Autowired
    FileConfig fileConfig;


    @Autowired
    FilesService filesService;

    @RequestMapping("fileUpload")
    public R fileUpload( Chunk chunk) throws InterruptedException {
        fileConfig.postFileUpload(chunk,"bumen");
        if (chunk.getChunkNumber().equals(chunk.getTotalChunks())){
            TimeUnit.MINUTES.sleep(1);
            FileResult bumen = fileConfig.mergeFile(chunk, "bumen");
            Files files = new Files();
            files.setName(files.getName());
            filesService.add(files);
            return R.successed(bumen);
        }
        return R.successed(null);
    }


}
